/*
 * Copyright (C) 2012 haru Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.harusource.di.delegate.impl;

import java.lang.reflect.Method;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.harusource.di.delegate.Delegate;

/**
 * This class implements AdapterView.OnItemSelectedListener.</br>
 * It handles following events of AdapterView.</br>
 * </br>
 * <code>
 * void onItemSelected(AdapterView<?> parent, View view, int position, long id)</br>
 * void onNothingSelected(AdapterView<?> parent)
 * </code>
 * </br>
 * </br>
 * @see android.widget.AdapterView.OnItemSelectedListener#onItemSelected(AdapterView, View, int, long)
 * @see android.widget.AdapterView.OnItemSelectedListener#onNothingSelected(AdapterView)
 * 
 * @author Hiroki Oizumi
 */
public class AdapterViewOnItemSelectedListener extends Delegate implements OnItemSelectedListener {
    private static final int EVENTHANDLE_ONITEMSELECTED = 1;

    private static final int EVENTHANDLE_ONNOTHINGSELECTED = 2;
    
    @Override
    public void add(Method template, Object target, Method eventhandle, View view) {
        super.add(template, target, eventhandle, view);
        ((AdapterView<?>) view).setOnItemSelectedListener(this);
    }

    @EventHandler(EVENTHANDLE_ONITEMSELECTED)
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Delegate.invoke4(mTarget, mEventHandles, EVENTHANDLE_ONITEMSELECTED, arg0, arg1, arg2, arg3);
    }

    @EventHandler(EVENTHANDLE_ONNOTHINGSELECTED)
    public void onNothingSelected(AdapterView<?> arg0) {
        Delegate.invoke1(mTarget, mEventHandles, EVENTHANDLE_ONNOTHINGSELECTED, arg0);
    }
}
